$(document).ready(function(){

    $(".register-frame").hide();
    $(".login").addClass("active");
    $(".login-frame").show();
    // $(".login-frame").hide();
    // $(".register-frame").show();


    $(".login").click(function () {
        $(".register-frame").hide();
        $(".register").removeClass("active");
        $(".login").addClass("active");
        $(".login-frame").show();
        $(".box").css("height", "55%");
    });

    $(".register").click(function () {
        $(".login-frame").hide();
        $(".login").removeClass("active");
        $(".register").addClass("active");
        $(".register-frame").show();
        $(".box").css("height", "74%");
    });

    var timer="";
    var nums=60;
    var validCode=true;//定义该变量是为了处理后面的重复点击事件
    $(".code").click(function () {
        var code=$('.code');
        if(validCode){
            code.text("发送中...");
            code.css("color", "rgb(170, 170, 170)");
            validCode=false;
            timer=setInterval(function(){
                if(nums>1){
                    nums--;
                    code.text(nums+"秒后重新发送");
                    code.css("color", "rgb(170, 170, 170)");
                }
                else{
                    clearInterval(timer);
                    nums=60;//重置回去
                    validCode=true;
                    code.css("color", "#e4393c");
                    code.text("发送验证码");
                }
            },1000)
        }
    });



//该变量判断验证码是否正确
var if_code_correct;

//后端返回的验证码
var compare_code;

//点击发送验证码按钮获取验证码
$("#verifi-btn2").click(function(){

    
    var user_email;
    var reg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");

    if($("#regis-email2").val()==""){
        alert("邮箱不能为空！")
        return;
    }
    else if($("#regis-email2").val()!==""){
        
        if(!reg.test($("#regis-email2").val())){
            alert("邮箱格式不正确，请重新输入。");
            return;
        }
        else{
            user_email=$("#regis-email2").val();
            
        }
    }

    var data={
        email:user_email,
    }
    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/getCheckCode", 
        data: data,
        ContentType: 'application/json;charset=utf-8',
        
        success: function(data) {   
           compare_code=data;
        //    console.log(data);
        },
        error: function(xhr, type) {
            console.log("Error");
        }
     });

});


//点击注册按钮
$("#register-btn2").click(function(){
   
    var user_email;
    var user_name;
    var user_pwd;
    var verifi_code;
    var reg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");

    if($("#regis-name2").val()==""){
        alert("用户名不能为空！")
        return;
    }
    else{
        user_name=$("#regis-name2").val();
    }

    if($("#regis-pwd2").val()==""){
        alert("密码不能为空！")
        return;
    }
    else{
        user_pwd=$("#regis-pwd2").val();
    }

    if($("#regis-email2").val()==""){
        alert("邮箱不能为空！")
        return;
    }
    else if($("#regis-email2").val()!==""){
        
        if(!reg.test($("#regis-email2").val())){
            alert("邮箱格式不正确，请重新输入。");
            return;
        }
        else{
            user_email=$("#regis-email2").val();
            
        }
    }

    if($("#regis-verifi-code2").val()==""){
        alert("验证码不能为空！")
        return;
    }
    else{
        verifi_code=$("#regis-verifi-code2").val();
    }

    if(!(verifi_code==compare_code)){
        alert("验证码错误！");
        return
    }
    else{
        var data={
            email:user_email,
            password:user_pwd,
            username:user_name,
            sex:0,
            phonenumber:"",
        }
        $.ajax({
            
            url: "http://8.130.103.251:8080/seller/sellerregister", 
            type: 'GET',
            data:data,
            ContentType: 'application/json;charset=utf-8',
            success: function(data) {  
                
                if(data.code==-1){
                    alert("用户名已存在！");
                }
                else if(data.code==0){
                    $.cookie('username', user_name, { expires: 7 });
                    $.cookie('if_user', 0, { expires: 7 });
                    // $.cookie('status',1, { expires: 7 });
                    window.location.href = "merchants.html";
                }
            //    console.log(data);
            //    $.cookie('username', user_name, { expires: 7 });
            //    $.cookie('if_user', 0, { expires: 7 });
            
            //    window.location.href = "merchants.html";
            },
            error: function(xhr, type) {
                console.log("Error");
            }

         });
    }


});

//点击登录按钮
$("#login-btn2").click(function(){
    var user_name;
    var user_pwd;

    if($("#login-name2").val()==""){
        alert("用户名不能为空！")
        return;
    }
    else{
        user_name=$("#login-name2").val();
    }

    if($("#login-pwd2").val()==""){
        alert("密码不能为空！")
        return;
    }
    else{
        user_pwd=$("#login-pwd2").val();
    }

    var data={
       
        password:user_pwd,
        username:user_name,
      
    }
    $.ajax({
        
        url: "http://8.130.103.251:8080/seller/sellerlogin", 
        type: 'GET',
        data:data,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
            if(data.code==-1){
                alert("用户或密码错误");
            }
            else if(data.code==0){
                $.cookie('username', user_name, { expires: 7 });
                $.cookie('if_user', 0, { expires: 7 });
                // $.cookie('status',1, { expires: 7 });
                window.location.href = "merchants.html";
            }
        },
        error: function(xhr, type) {
            console.log("Error");
        }
     });
 
})

});